package com.ctgu.edu.controller;

import com.ctgu.edu.pojo.*;
import com.ctgu.edu.service.impl.ApplyServiceImpl;
import com.ctgu.edu.service.impl.JobServiceImpl;
import com.ctgu.edu.service.impl.ResumeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.LinkedList;

@Controller
public class JobAndApplyController {

    @Autowired
    @Qualifier("jobServiceImpl")
    private JobServiceImpl jobServiceImpl;
    @Autowired
    @Qualifier("applyServiceImpl")
    private ApplyServiceImpl applyServiceImpl;
    @Autowired
    @Qualifier("resumeServiceImpl")
    private ResumeServiceImpl resumeServiceImpl;

    @RequestMapping("/job/detail/{jobId}")
    public String JobDetail(@PathVariable Integer jobId, Model model){
        Job job = jobServiceImpl.getJobById(jobId);
        job.setJobClickTimes(job.getJobClickTimes()+1);
        jobServiceImpl.updateJob(job);
        model.addAttribute("job",job);


        return "job_main_page";
    }

    //提交申请
    @RequestMapping("/job/apply/{jobId}")
    public String JobApply(@PathVariable Integer jobId, Model model, HttpServletRequest request){
        Apply apply = new Apply();
        HttpSession session = request.getSession();
        Seeker seeker =(Seeker) session.getAttribute("seeker"); //获取当前登录的用户

        Apply existApply = applyServiceImpl.getApplyBySeekerAndJobId(seeker.getSeekerId(),jobId);
        if(existApply != null){
            model.addAttribute("inf_seeker","请勿重复提交申请!!");
            return "forward:/seeker/mainpage/0/10";
        }
        Resume resume;
        resume = resumeServiceImpl.getResumeByOwnerId(seeker.getSeekerId());
        if(resume == null){
            model.addAttribute("inf_edit_resume","请先编辑简历");
            return "forward:/welcome/edit_resume";
        }

        apply.setApplySeekerId(seeker.getSeekerId());
        apply.setApplyJobId(jobId);
        apply.setApplyFlag(0);
        apply.setApplySalary(0);
        applyServiceImpl.addApply(apply);
        model.addAttribute("inf_seeker","申请成功!!");
        return "forward:/seeker/mainpage/0/10";
    }
}
